﻿CREATE FUNCTION [dbo].[hist_Tracker__Last]
(	@TrackerID varchar(11)
)RETURNS TABLE
AS
RETURN 
SELECT     TECH.TechnicianID_Last, TECH.Technician_Last, TRACKER.TrackerID, TRACKER.Timer
FROM         (SELECT     LAST_TECH.TechnicianID AS TechnicianID_Last, v_Technician.Name AS Technician_Last
                       FROM          v_Technician INNER JOIN
                                                  (SELECT     htt.TechnicianID
                                                    FROM          (SELECT     MAX(HID) AS HID
                                                                            FROM          vhist_t_Technician_Tracker
                                                                            WHERE      (TrackerID = @TrackerID)) AS LAST INNER JOIN
                                                                           vhist_t_Technician_Tracker AS htt ON LAST.HID = htt.HID) AS LAST_TECH ON v_Technician.ID = LAST_TECH.TechnicianID) 
                      AS TECH FULL OUTER JOIN
                          (SELECT     LAST.TrackerID, dbo.Timer(v_Status_Tracker_1.DateStamp) AS Timer
                            FROM          (SELECT  TrackerID, MAX(HID) AS HID
                                                    FROM          v_Status_Tracker
                                                    WHERE      (TrackerID = @TrackerID)
                                                    GROUP BY TrackerID) AS LAST INNER JOIN
                                                   v_Status_Tracker AS v_Status_Tracker_1 ON LAST.HID = v_Status_Tracker_1.HID) AS TRACKER ON 1 = 1



